﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Billing.Utilities;

namespace Billing.Windows
{
    /// <summary>
    /// Interaction logic for PayType.xaml
    /// </summary>
    public partial class PayType : Window
    {
        List<string> payTypes = new List<string>();
        BillingGrid m_BillingGrid;

        public PayType(BillingGrid billingGrid)
        {
            try
            {
                InitializeComponent();

                m_BillingGrid = billingGrid;
                payTypeCashAmount.Text = m_BillingGrid.PaytypeCashAmount == 0 ? m_BillingGrid.TotalNetAmount.ToString() : m_BillingGrid.PaytypeCashAmount.ToString();
                payTypeCardAmount.Text = m_BillingGrid.PaytypeCardAmount.ToString();
                payTypeSodexoAmount.Text = m_BillingGrid.PaytypeSodexoAmount.ToString();
                payTypeCardAmount.Focus();
                payTypeCardAmount.SelectAll();
            }
            catch (Exception) { }
        }

        public PayType(BookCake bookCake)
        {
 
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Escape)
                {
                    this.Close();
                }
                else if (e.Key == Key.Enter)
                {
                    if (payTypeSodexoAmount.Text.Trim() == string.Empty || !payTypeSodexoAmount.IsFocused)
                    {
                        //payTypeSodexoAmount.Focus();
                        //payTypeSodexoAmount.SelectAll();
                    }
                    else if (payTypeCardAmount.Text.Trim() == string.Empty)
                    {
                        //payTypeCardAmount.Focus();
                    }
                    else
                    {
                        m_BillingGrid.SetPayType(decimal.Parse(payTypeCardAmount.Text.ToString()), decimal.Parse(payTypeSodexoAmount.Text.ToString()));
                        this.Close();
                    }
                }
                else
                {

                }
            }
            catch (Exception) { }
        }

        private void payTypeCardAmount_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Down && (sender as TextBox).Name == "payTypeCardAmount")
                {
                    payTypeSodexoAmount.Focus();
                    payTypeSodexoAmount.Select(0, payTypeSodexoAmount.Text.Length);
                }
                else if (e.Key == Key.Up && (sender as TextBox).Name == "payTypeSodexoAmount")
                {
                    payTypeCardAmount.Focus();
                    payTypeCardAmount.Select(0, payTypeCardAmount.Text.Length);
                }
                if (payTypeCardAmount.Text.Trim() != string.Empty && payTypeSodexoAmount.Text.Trim() != string.Empty)
                    payTypeCashAmount.Text = (m_BillingGrid.TotalNetAmount - (decimal.Parse(payTypeCardAmount.Text.ToString()) + decimal.Parse(payTypeSodexoAmount.Text.ToString()))).ToString();
            }
            catch (Exception)
            {
                payTypeCardAmount.Text = string.Empty;
                payTypeCashAmount.Text = string.Empty;
            }
        }
    }
}
